var express = require('express');
var router = express.Router();

var https = require('https');

var async = require('async');

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = "mongodb://localhost:27017/Movie";

router.get('/', function (req, res) {
  async.parallel({
    responseData: function (cb) {
      console.log(req.query.movie_id);
      var requestUrl = 'https://api.douban.com/v2/movie/subject/' + req.query.movie_id;
      var responseData = '';
      https.get(requestUrl, function (res) {
        var responseData = '';
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
          responseData += chunk;
        });
        res.on('end', function () {
        var datailData = JSON.parse(responseData);
        console.log(datailData);
        cb(null, datailData);
      })
      });
      
    },
    session: function (cb) {
      cb(null, req.session.email);
    }
  }, function (err, results) {
    console.log(results);
    res.render('comment', {
      movie: {
        responseData: results.responseData,
        email: results.session
      }
    });
  });
});

module.exports = router;